package cn.itsource.xhy.pro.controller;

import cn.itsource.xhy.org.domain.Employee;
import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.pet.domain.Pet;
import cn.itsource.xhy.pet.query.PetQuery;
import cn.itsource.xhy.pro.domain.Product;
import cn.itsource.xhy.pro.query.ProductQuery;
import cn.itsource.xhy.pro.service.IProductService;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.LoginContext;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/pro")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PostMapping
    public QueryResult<Product> queryPage(@RequestBody ProductQuery productQuery){
        return productService.queryPage(productQuery);
    }

    @PostMapping("/front")
    public QueryResult<Product> queryPageByFront(@RequestBody ProductQuery productQuery, HttpServletRequest request){
        return productService.queryPage(productQuery);
    }

    @GetMapping("/detail/{id}")
    public Product findByProId(@PathVariable("id") Long id){
        return productService.findByProId(id);
    }

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product){
        try {
            if(product.getId()==null){
                productService.save(product);
            } else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids){
        try {
            productService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PatchMapping("/downSale")
    public AjaxResult downSale(@RequestBody Long[] ids){
        try {
            productService.downSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

}
